package com.cvte.boe.media.dao;

import android.content.Context;
import android.content.SharedPreferences;

import com.cvte.boe.media.MediaApplication;
import com.cvte.boe.media.utils.SettingUtil;

/**
 * Created by wuguitong on 2018/5/4.
 */
public class MediaSettingDaoImpl implements MediaSettingDao{
    private static final String SETTINGFILE = "MediaSettings";
    private SharedPreferences mSharedPreferences;
    private Context mContext;


    public MediaSettingDaoImpl(Context mContext) {
        this.mContext = mContext;
        mSharedPreferences = MediaApplication.getAppContext().getSharedPreferences(SETTINGFILE, Context.MODE_PRIVATE);
    }

    @Override
    public SettingUtil.ILOOP getImageLoop() {
        return SettingUtil.ILOOP.valueOf(load(SettingUtil.IMAGELOOP,DEFILOOP));
    }

    @Override
    public void setImageLoop(SettingUtil.ILOOP loop) {
        save(SettingUtil.IMAGELOOP,loop.name());
    }

    @Override
    public SettingUtil.ILOOPTIMER getImageLoopTimer() {
        return SettingUtil.ILOOPTIMER.valueOf(load(SettingUtil.IMAGELOOPTIMER,DEFILOOPTIMER));
    }

    @Override
    public void setImageLoopTimer(SettingUtil.ILOOPTIMER timer) {
        save(SettingUtil.IMAGELOOPTIMER,timer.name());
    }

    @Override
    public SettingUtil.MPLAYMODE getMusicPlayMode() {
        return SettingUtil.MPLAYMODE.valueOf(load(SettingUtil.MUSICPLAYMODE, DEFMPLAYMODE));
    }

    @Override
    public void setMusicPlayMode(SettingUtil.MPLAYMODE mode) {
        save(SettingUtil.MUSICPLAYMODE,mode.name());
    }

    @Override
    public SettingUtil.VLOOP getVideoLoop() {
        return SettingUtil.VLOOP.valueOf(load(SettingUtil.VIDEOLOOP,DEFVLOOP));
    }

    @Override
    public void setVideoLoop(SettingUtil.VLOOP loop) {
        save(SettingUtil.VIDEOLOOP,loop.name());
    }

    @Override
    public SettingUtil.VSUBTITLE getVideoSubtitle() {
        return SettingUtil.VSUBTITLE.valueOf(load(SettingUtil.VIDEOSUBTITLE,DEFVSUBTITLE));
    }

    @Override
    public void setVideoSubtitle(SettingUtil.VSUBTITLE subtitle) {
        save(SettingUtil.VIDEOSUBTITLE,subtitle.name());
    }

    @Override
    public SettingUtil.VAUDIO getVideoAudio() {
        return SettingUtil.VAUDIO.valueOf(load(SettingUtil.VIDEOAUDIO,DEFVAUDIO));
    }

    @Override
    public void setVideoAudio(SettingUtil.VAUDIO audio) {
        save(SettingUtil.VIDEOAUDIO,audio.name());
    }

    @Override
    public SettingUtil.VDISPLAY getVideoDisplay() {
        return SettingUtil.VDISPLAY.valueOf(load(SettingUtil.VIDEODISPLAY,DEFVDISPLAY));
    }

    @Override
    public void setVideoDisplay(SettingUtil.VDISPLAY display) {
        save(SettingUtil.VIDEODISPLAY,display.toString());
    }
    private void save(String key,String value){
        synchronized (this){
            SharedPreferences.Editor ed = mSharedPreferences.edit();
            ed.putString(key,value);
            ed.commit();
        }
    };

    private String load(String key,String def){
        synchronized (this) {
            return mSharedPreferences.getString(key, def);
        }
    }
}
